<?php
/*
*
* @author rana
*
*/

// Strip out javascript, styles etc..
$GLOBALS['search_regexp'] = array ('@<script[^>]*?>.*?</script>@si','@<styles[^>]*?>.*?</styles>@si');              
$GLOBALS['replace_regexp'] = array ('','');
$GLOBALS['READ_MAX_FILESIZE'] = 102400;

class urlreader {

	function urlreader() {
	}
	
	//
	function read($url,$short_url) {

		if (checkrobots($short_url) == false) {
			$headers[0] = '401 Robots';
			return $headers;
		}

		$contents = file_get_contents($url);
		$headers[0] = '404';

		if ($contents !== false) {

			$contents = preg_replace($GLOBALS['search_regexp'], $GLOBALS['replace_regexp'], $contents);
			$contents = str_replace("\n"," ",$contents);
			$headers[0] = '200';
		}
		$headers['Last-Modified'] = date("Y-m-dTG:i:s+00:00");//2006-10-15T11:36:36+00:00

		return array($headers, $contents);
		//return $contents;
	}

	function get_headers_only($url,$short_url) {
		if (FSOCK_ENABLED)
			return $this->get_headers_fsock($url,$short_url);
		else
			return $this->get_headers($url,$short_url);
	}

	// get_headers_only
	function get_headers($url,$short_url) {

		if (checkrobots($short_url) == false) {
			$headers[0] = '401 Robots';
			return $headers;
		}

		$handle = @fopen($url, "r");
		$headers[0] = '404';

		if ($handle !== false) {

			$headers[0] = '200';
		}
		fclose($handle);
		$headers['Last-Modified'] = date("Y-m-dTG:i:s+00:00");//2006-10-15T11:36:36+00:00

		return $headers;
	}

	// get_headers_only
	function get_headers_fsock($url,$short_url) {
		
		if (checkrobots($short_url) == false) {
			$headers[0] = '401 Robots';
			return $headers;
		}

		$urlinfo = parse_url($url);
		if (empty($urlinfo['port'])) $urlinfo['port'] = 80;
		if (empty($urlinfo['path'])) $urlinfo['path'] = '/';
		$headers[0] = '408 Time Out';

		$fp = fsockopen($urlinfo['host'], $urlinfo['port'], $errno, $errstr, 60);
		if (!$fp) {
			$headers[0] = "$errno $errstr";
			debug("$errstr ($errno)<br />\n");
		} else {
			$out = "GET ".$urlinfo['path']." HTTP/1.1\r\n";
			$out .= "Host: ".$urlinfo['host']."\r\n";
			$out .= "Connection: Close\r\n\r\n";

			fwrite($fp, $out);
			$headers = array();
			while (!feof($fp)) {
				 $line = fgets($fp, 1024);
				 if ( empty($headers) || strpos($line,':') !== false || empty($line) ) {
					 if ( empty($headers) )
						 $headers[0] = $line;
					 else {
						 $lines = explode(':', $line);
						 $headers[trim($lines[0])] = trim($lines[1]);
					 }
				 } else
					 break;
			}
			fclose($fp);
		}
		if(!isset($headers['Last-Modified']))
			$headers['Last-Modified'] = date("Y-m-dTG:i:s+00:00");//2006-10-15T11:36:36+00:00
		return $headers;
	}

	//get headers as well as contents
	function read_data($url,$short_url) {
		
		if (FSOCK_ENABLED)
			return $this->read_fsock($url,$short_url);
		else
			return $this->read($url,$short_url);
	}

	//get headers as well as contents
	function read_fsock($url,$short_url) {
		
		if (checkrobots($short_url) == false) {
			$headers[0] = '401 Robots';
			return array($headers, '');
		}

		$urlinfo = parse_url($url);
		if (empty($urlinfo['port'])) $urlinfo['port'] = 80;
		if (empty($urlinfo['path'])) $urlinfo['path'] = '/';
		$headers[0] = '408 Timeout';

		$fp = fsockopen($urlinfo['host'], $urlinfo['port'], $errno, $errstr, 60);
		if (!$fp) {
			debug("$errstr ($errno)<br />\n");
			$headers[0] = $errno . ' ' . $errstr;
		} else {
			$out = "GET ".$urlinfo['path']." HTTP/1.1\r\n";
			$out .= "Host: ".$urlinfo['host']."\r\n";
			$out .= "Connection: Close\r\n\r\n";

			fwrite($fp, $out);

			$headers = array();
			$contents = '';
			$length = 1024;

			while (!feof($fp)) {
				if( $length == 1024 )
					$line = fgets($fp, $length);
				else
					$line = fread($fp, $length);

				 if ( empty($headers) && $length == 1024) {

					 $headers[0] = $line;
				 } else if ( strpos($line,':') !== false && $length == 1024 ) {

					 $lines = explode(':', $line);
					 $headers[trim($lines[0])] = trim($lines[1]);
				 } else {
					 $contents .= $line;
					 if(!empty($headers['Content-Length']))
						 $length = $headers['Content-Length'];
					 else
						$length = 2 * 8192;
				 }
				 if (isset($headers['Content-Length']) && $headers['Content-Length'] > $GLOBALS['READ_MAX_FILESIZE']) {
					fclose($fp);
					return array('header' => $headers, 'content' => '');
				} 
			}
			fclose($fp);
		}
		if(!isset($headers['Last-Modified']))
			$headers['Last-Modified'] = date("Y-m-dTG:i:s+00:00");//2006-10-15T11:36:36+00:00

		$contents = preg_replace($GLOBALS['search_regexp'], $GLOBALS['replace_regexp'], $contents);

		$contents = str_replace("\n"," ",$contents);

		return array($headers, $contents);
	}

	//
}
?>